package com.todod.base.module.base.controller.admin.rtdistribute;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.todod.base.framework.common.pojo.PageParam;
import com.todod.base.framework.common.pojo.PageResult;
import com.todod.base.framework.common.pojo.CommonResult;
import com.todod.base.framework.common.util.object.BeanUtils;
import static com.todod.base.framework.common.pojo.CommonResult.success;

import com.todod.base.framework.excel.core.util.ExcelUtils;

import com.todod.base.framework.apilog.core.annotation.ApiAccessLog;
import static com.todod.base.framework.apilog.core.enums.OperateTypeEnum.*;

import com.todod.base.module.base.controller.admin.rtdistribute.vo.*;
import com.todod.base.module.base.dal.dataobject.rtdistribute.RtDistributeDO;
import com.todod.base.module.base.service.rtdistribute.RtDistributeService;

@Tag(name = "管理后台 - 实时数据分发")
@RestController
@RequestMapping("/base/rt-distribute")
@Validated
public class RtDistributeController {

    @Resource
    private RtDistributeService rtDistributeService;

    @PostMapping("/create")
    @Operation(summary = "创建实时数据分发")
    public CommonResult<Long> createRtDistribute(@Valid @RequestBody RtDistributeSaveReqVO createReqVO) {
        return success(rtDistributeService.createRtDistribute(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新实时数据分发")
    public CommonResult<Boolean> updateRtDistribute(@Valid @RequestBody RtDistributeSaveReqVO updateReqVO) {
        rtDistributeService.updateRtDistribute(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除实时数据分发")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteRtDistribute(@RequestParam("id") Long id) {
        rtDistributeService.deleteRtDistribute(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得实时数据分发")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<RtDistributeRespVO> getRtDistribute(@RequestParam("id") Long id) {
        RtDistributeDO rtDistribute = rtDistributeService.getRtDistribute(id);
        return success(BeanUtils.toBean(rtDistribute, RtDistributeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得实时数据分发分页")
    public CommonResult<PageResult<RtDistributeRespVO>> getRtDistributePage(@Valid RtDistributePageReqVO pageReqVO) {
        PageResult<RtDistributeDO> pageResult = rtDistributeService.getRtDistributePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RtDistributeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出实时数据分发 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRtDistributeExcel(@Valid RtDistributePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RtDistributeDO> list = rtDistributeService.getRtDistributePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "实时数据分发.xls", "数据", RtDistributeRespVO.class,
                        BeanUtils.toBean(list, RtDistributeRespVO.class));
    }

}